<?php

   defined('ACC') || exit('Acc deined');


   class userModel extends Model{
   	    protected $table = 'user';
   	    protected $pk = 'user_id';
   	    public $fields = array('user_id','username','email','passwd','regtime','lastlogin');


		 /**
		      验证规则, 参数1: 验证的字段名
		               参数2: 验证规则 0:可以不填，但是填了必须满足规则 1:必填 2:如果有，且内容不为空，则检查， 
		               参数3：错误信息
		               参数4: 具体规则
		               参数5: 具体规则对应的数据
		 */
        protected $rule = array(
            array('username',1,'用户名不能为空且唯一','unique',''),
            array('email',1,'邮箱必填且必须符合邮箱规则','email',''),
            array('passwd',1,'密码不能为空','required',''),
        );


        //用户注册
        public function reg($data){
        	if(isset($data['passwd'])){
        		 $data['passwd'] = $this->mdpasswd($data['passwd']);
        	}
        	return $this->add($data);
        }
        
        //把密码转换为md5加密格式
        protected function mdpasswd($p){
        	return md5($p);
        }


        //查询是否有该条登陆信息
        public function checkmsg($username,$passwd=''){
          //如果没有，则查询输入的用户名对应的用户信息
          $sql = 'select user_id,username,email,passwd from user where username = "' . $username . '"';
           
          $data = $this->db->getrow($sql);
          if(!empty($passwd)){
               //判断该用户登陆信息是否正确
             if($data['passwd'] != $this->mdpasswd($passwd)){
                return false;
             }
          }
          unset($data['passwd']);
          return $data;
        }
        
   }



